<!--suppress ALL -->
<template>
<div class="page maintainTask-page" data-page="linkTestTask" style="z-index: 2">
	<div class="link-page-nav">
		<a href="#" class="link" @click=$root.backToTab()>
			<i class="iconfont icon-zuo"></i>
		</a>
	</div>
	<div class="link-page-top">
		<span class="link-page-dan" id="isDan_{{getNewId}}">单</span>
		<div class="link-page-title">联动测试作业</div>
		<a style="text-align: center; top: 0" @click="createDangers()">
			<i class="iconfont icon-APPicon_tianjia" style="font-size: 9px; color: #222222;"></i>
			<p style="font-size: 9px; color: #222222;">创建隐患<p>
		</a>
	</div>
	<div class="page-content" style="padding-bottom: 100px">
		<div class="mine-margin">
			<span class="tt">作业对象</span>

			<div class="workObj">
				<span style="float: left; color: #999999">消防系统</span> <span
					style="float: right; color: #333333" id="systemName_{{getNewId}}"></span>
			</div>

		</div>

		<div class="over-line"></div>

		<div class="mine-margin">
			<span class="tt">作业标准</span>
			<p class="workContent" id="workStandardView_{{getNewId}}"></p>
		</div>

		<div class="over-line"></div>

		<div class="mine-margin">
			<span class="tt">作业前拍照</span>
			<div style="margin-top: 20px;" id="picBeforeUrl_{{getNewId}}"></div>
		</div>

		<div class="mine-margin" style="margin-top: 50px" >
			<span class="tt">作业后拍照</span>
			<div style="margin-top: 20px;" id="picAfterUrl_{{getNewId}}"></div>
		</div>

		<div class="mine-margin RelationPolice1 RelationPolice2" style="margin-top: 50px">
			<span class="tt">关联警情</span>
			<p class="hint">系统自动关联可能由当前联动测试引起的设备报警信息。如果和实际不一致，请修改。</p>
			<ul id="alarmList_{{getNewId}}"></ul>
			<div class="deviceHint">
				<span id="deviceHintInfoView_{{getNewId}}"></span>
			</div>
			<div>共发生警情<span style="margin-left:8px" id="alarmTotal_{{getNewId}}"></span>
				<a href="#">
					<label @click="correlatedAlarm()">修改</label>
					<i class="iconfont icon-wulumuqishigongandashujuguanlipingtai-ico-"></i>
				</a>
			</div>
			<div class="hr"></div>
		</div>

		<div class="mine-margin" style="margin-top: 50px">
			<span class="tt">作业填报</span>
			<div id="workContentListView_{{getNewId}}"></div>
		</div>
		<div class="submitBtn" @click="upInfoData()">提交</div>
	</div>
</div>
</template>

<script>
	return {
		mounted() {
            var self = this;
            var app = self.$app;
            //页面传参
            self.taskId=this.$route.params.taskId;
        },
		data : function() {
			return {
				getNewId: (new Date()).getTime(),
				//index : 0,//上传图片下标
				index:"",
				//taskId:1,
				workContentList:null,
				flag:1, // 显示更多的图片
				testType:4,
				relateAlarmIds:"0",
				detailId:'',
				fbsId:null,
				orgId:null,
			}
		},
		methods : {
			createDangers:function(){
				if(native.getDataByKey("IS_OFFLINE_MODULE") == 1){
					app.methods.showToastCenter("离线模式下不能跳转");
					return
				}else{
					app.router.navigate("/createTrouble/-1/"+this.$route.params.taskId+"/-1/");
				}
	        },
	        correlatedAlarm:function(){//跳转到关联的警情  type 0  查看  1编辑
        		var self = this;
				if(native.getDataByKey("IS_OFFLINE_MODULE") == 1){
					app.methods.showToastCenter("离线模式下不能跳转");
					return
				}else{
					app.router.navigate("/correlatedAlarm/"+self.taskId+"/1/"+self.detailId+"/"+self.testType+"/"+self.relateAlarmIds+"/linkTestTask");
				}
	        },
			getAlarmForTest:function(){
	        	var self = this;
	        	common.loading(0);
	        	$("#deviceHintInfo_" + self.getNewId).parent().hide();
	        	$("#alarmList_" + self.getNewId).hide();
	        	Dao.getAlarmForTest({
	        		account:userInfor.accountName,
	        		userName:userInfor.accountName,
                    deviceId:appKeyObj.deviceId,
                    taskDetailId:self.taskId,
                    type:self.testType.toString(),
	        	},function(data,total){
	        		var alarmTotal=0;
	        		if(total>0){
		        		if (data!=null&&data.length != 0) {
		        			$("#alarmList_" + self.getNewId).show();
		        			self.relateAlarmIds=data[0].relateAlarmIds;
		        			for(var i=0;i<data.length;i++){
		        				var icons=JSON.parse(data[i].iconUrl).icon;
		        				 if(data[i].fireTTypes=="10"){
			        				$("#alarmList_" + self.getNewId).append(`<li>
			        						<div class="img"><img src="`+icons+`"></div>
			        						<span>`+data[i].fcFacilitiesCategoryName+`</span>
			        						<span class="times"><label style="color:#E07762">报警 </label>×<label>`+data[i].times+`</label>次</span>
			        					</li>`);
		        			 	}else{
		        					$("#alarmList_" + self.getNewId).append(`<li>
		        							<div class="img"><img src="`+icons+`"></div>
			        						<span>`+data[i].fcFacilitiesCategoryName+`</span>
			        						<span class="times"><label style="color:#FFC73B">故障 </label>×<label>`+data[i].times+`</label>次</span>
			        					</li>`);
		        				}
		        				 alarmTotal=alarmTotal+data[i].times;
		        			}

		        		}
	        		}else{
	        			$("#deviceHintInfo_" + self.getNewId).parent().show();
	        			$("#deviceHintInfoView_" + self.getNewId).empty().html(common.transNullundefinedToline(data.info));
	        		}
	        		$("#alarmTotal_" + self.getNewId).empty().html(alarmTotal+"次");
	        	});
	        },
			getInfoData:function(){//获取作业信息
				var self = this;
				//common.loading(1);

				// 上传现场图片
				jQuery(".pic").off().click(function(){
					self.index = this;
					self.flag = 0;
					jQuery("#upLoadPickfiles").click();
				});

				// 更多上传图片
				jQuery(".morePic").off().click(function(){
					self.index = this;
					self.flag = 1;
					jQuery("#upLoadPickfiles").click();
				});

			   if(self.businessType==1){
					$("#isDan_" + self.getNewId).show();
					$("#assetsNameView_" + self.getNewId).show();
					$("#finishNumView_" + self.getNewId).hide();
				}else{
					$("#isDan_" + self.getNewId).hide();
					$("#assetsNameView_" + self.getNewId).hide();
					$("#finishNumView_" + self.getNewId).show();
				}
				Dao.getMaintainTaskDetailInfoDtoForLD({
					userName:userInfor.accountName,
                    deviceId:appKeyObj.deviceId,
                    taskId:self.taskId,//任务id
				},function (data){
					common.loading(0);
					if (data!=null&&data.length != 0) {
						self.fbsId = data.fbsId;
						self.orgId = data.orgId;
						self.detailId = data.id;
						self.workContentList=data.maintainStandardContentList;
						$("#systemName_" + self.getNewId).empty().html(common.transNullundefinedToline(data.fireSystemName));
						$("#workStandardView_" + self.getNewId).empty().html(common.transNullundefinedToline(data.workStandard));
						for (var i=0;i<self.workContentList.length;i++) {
							var bean=self.workContentList[i];
							var inputHtml="";
							var checkHtml=`
								<p class="buttons-row" style="margin-top: 15px;">
									<div class="checkItem">
									 	<input type="radio" id="normal" name="checkRadio_`+i+`" value="0" checked="checked">
									 	<label name="normal" class="option-`+bean.id+` checked" for="normal">正常</label>
								 	</div>
								 	<div class="checkItem" style="margin: 0 3%;">
									 	<input type="radio" id="abnormal" name="checkRadio_`+i+`" value="1" >
									 	<label name="abnormal" class="option-`+bean.id+`" for="abnormal">异常</label>
								 	</div>
								 	<div class="checkItem">
									 	<input type="radio" id="notInvoled" name="checkRadio_`+i+`" value="2">
									 	<label name="notInvoled" class="option-`+bean.id+`" for="notInvoled">未涉及</label>
								 	</div>
								</p>`;
							var decHtml=`
								<p style="margin-top: 15px;">
									<input type="text" class="des-`+bean.id+` inDes" id="inDes_`+i+`" placeholder="在这里填写清晰明了的描述" />
								</p>`;
							var numHtml=`
								<p style="margin-top: 15px;">
								<input type="number" class="num-`+bean.id+` inNum" id="inNum_`+i+`" placeholder="请输入数值" />
							</p>`
							switch(bean.type){//1：单选，2：文本，3：数值，4：单选+文本，5：单选+数值，6：数值+文本
								case 1:inputHtml=checkHtml;
									break;
								case 2:inputHtml=decHtml;
									break;
								case 3:inputHtml=numHtml;
									break;
								case 4:inputHtml=checkHtml+decHtml;
									break;
								case 5:inputHtml=checkHtml+numHtml;
									break;
								case 6:inputHtml=numHtml+decHtml;
									break;
							}
							$("#workContentListView_" + self.getNewId).append(`
								<div>
									<p class="workContent">`+bean.workContent+`</p>
									`+inputHtml+`
								</div>
							`);
						    $("#workContentListView_" + self.getNewId+ " .option-"+bean.id).click(function(){
						    	let conClass = this.classList[0];
							    let radioId = $(this).attr('name');
							    $('.'+conClass).removeClass('checked');
							    $(this).addClass('checked');
						    });
						}
						self.getAlarmForTest();
					}
				});
			},
			//提交数据
			upInfoData:function(){
				var self = this;
				//获取图片
				//作业前
				let beforPic = $("#picBeforeUrl_" + self.getNewId +" .pic-item");
				let beforImg = [];
				jQuery.each(beforPic, function (index, el) {
					let src = jQuery(el).attr("data-pic");
					beforImg.push(src);
				});
				// if (beforImg.length == 0) {
				// 	app.methods.showToastCenter("作业前的图片至少提交一张");
				// 	return;
				// }
				//作业后
				let afterPic = $("#picAfterUrl_" + self.getNewId +" .pic-item");
				let afterImg = [];
				jQuery.each(afterPic, function (index, el) {
					let src = jQuery(el).attr("data-pic");
					afterImg.push(src);
				});
				// if (afterImg.length <6) {
				// 	app.methods.showToastCenter("作业后的图片至少提交6张");
				// 	return;
				// }
				let flag =false
				common.loading(1);
				var jsonStr={};
				for (var i=0;i<self.workContentList.length;i++) {
					var bean=self.workContentList[i];
					var valStr={};
					switch (bean.type) {
	                    case 1:
	                        v1 = $("#workContentListView_" + self.getNewId+ " .option-"+bean.id+".checked").prev().val();
	                        valStr["tab"] = v1;
	                        break;
	                    case 2:
	                        v1 = $("#workContentListView_" + self.getNewId+ " .des-" + bean.id).val();
	                        valStr["des"] = v1;
	                        break;
	                    case 3:
	                        v1 = $("#workContentListView_" + self.getNewId+ " .num-" + i).val();
	                        valStr["num"] = v1;
	                        break;
	                    case 4:
	                        v1 = $("#workContentListView_" + self.getNewId+ " .option-"+bean.id+".checked").prev().val();
	                        v2 = $("#workContentListView_" + self.getNewId+ " .des-" + bean.id).val();
	                        valStr["tab"] = v1;
	                        valStr["des"] = v2;
	                        break;
	                    case 5:
	                        v1 = $("#workContentListView_" + self.getNewId+ " .option-"+bean.id+".checked").prev().val();
	                        v2 = $("#workContentListView_" + self.getNewId+ " .num-" + i).val();
	                        valStr["tab"] = v1;
	                        valStr["num"] = v2;
	                        break;
	                    case 6:
	                        v1 = $("#workContentListView_" + self.getNewId+ " .des-" + bean.id).val();
	                        v2 = $("#workContentListView_" + self.getNewId+ " .num-" + i).val();
	                        valStr["des"] = v1;
	                        valStr["num"] = v2;
	                        break;
					}
					if(valStr.tab == 1){//正常未涉及不需要校验文本框，只有异常需要校验文本框必填
						if(valStr.des == ''){
							flag = true
						}
					}
					jsonStr[bean.id]=valStr;
				}

				//作业填报
				let isNotFill = false;
				let work = $("#workContentListView_" + self.getNewId + " input");
				for(let j=0;j<work.length;j++){
					if(work[j].value == "" && flag){
						isNotFill = true;
						break;
					}
				}
				if(isNotFill){
					app.methods.showToastCenter("请填写作业填报内容");
					common.loading(0);
					return;
				}
				let paramData = {
					userName:userInfor.accountName,
					deviceId:appKeyObj.deviceId,
					taskId:self.taskId,//任务id
					picBeforeUrls:beforImg.toString(),
					picAfterUrls:afterImg.toString(),
					maintainAccount:userInfor.accountName,
					description:JSON.stringify(jsonStr),
					workType:1,
				};
				let IS_OFFLINE_MODULE = native.getDataByKey("IS_OFFLINE_MODULE");
				if (IS_OFFLINE_MODULE == "1"){
					native.updateMaintanceLocalData(paramData,self.fbsId,self.orgId,4);
				}
				Dao.postTaskWorkInfo(paramData,function(data){
					app.methods.backToTab();
					app.ptr.refresh();
					common.loading(0);
					app.methods.showToastCenter(data.info);
				});
			},
			//关联警情修改选择之后回调
			getSelectAlarmIdByLink:function(){
				var self = this;
				common.loading(0);
				$("#deviceHintInfo_" + self.getNewId).parent().hide();
				$("#alarmList_" + self.getNewId).hide().empty();
				Dao.getRelateAlarmCountByTaskId({
					taskId:self.taskId
				},function(data,total){
	        		var alarmTotal=0;
	        		if(total>0){
		        		if (data!=null&&data.length != 0) {
		        			$("#alarmList_" + self.getNewId).show();
		        			self.relateAlarmIds=data[0].relateAlarmIds?data[0].relateAlarmIds:"0";
		        			for(var i=0;i<data.length;i++){
		        				var icons=JSON.parse(data[i].iconUrl).icon;
		        				 if(data[i].fireTTypes=="10"){
			        				$("#alarmList_" + self.getNewId).append(`<li>
			        						<div class="img"><img src="`+icons+`"></div>
			        						<span>`+data[i].fcFacilitiesCategoryName+`</span>
			        						<span class="times"><label style="color:#E07762">报警 </label>×<label>`+data[i].times+`</label>次</span>
			        					</li>`);
		        			 	}else{
		        					$("#alarmList_" + self.getNewId).append(`<li>
		        							<div class="img"><img src="`+icons+`"></div>
			        						<span>`+data[i].fcFacilitiesCategoryName+`</span>
			        						<span class="times"><label style="color:#FFC73B">故障 </label>×<label>`+data[i].times+`</label>次</span>
			        					</li>`);
		        				}
		        				 alarmTotal=alarmTotal+data[i].times;
		        			}

		        		}
	        		}else{
						$("#deviceHintInfo_" + self.getNewId).parent().show();
	        			$("#deviceHintInfoView_" + self.getNewId).empty().html(common.transNullundefinedToline(data.info));
	        		}
	        		$("#alarmTotal_" + self.getNewId).empty().html(alarmTotal+"次");
				})
			}
		},
		on : {
			pageInit : function(e, page) {
				var self = this;
				var $ = self.$;
				self.getInfoData();
				//初始化上传
				upLoadImagesCompt(jQuery("#picBeforeUrl_" + self.getNewId),function(pic,isAdd){},true,null,'picBeforeUrl_container_' + self.getNewId,'picBeforeUrl_pick_' + self.getNewId,"上传图片",true);
				upLoadImagesCompt(jQuery("#picAfterUrl_" + self.getNewId),function(pic,isAdd){},true,null,'picAfterUrl_container_' + self.getNewId,'picAfterUrl_pick_' + self.getNewId,"上传图片",true);
			},
			pageAfterIn : function(e, page) {
				var self = this;
				var $ = self.$;
			},
		}
	}
</script>
